package com.example.paging.ui

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.webkit.JavascriptInterface
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import com.example.paging.R
import kotlinx.android.synthetic.main.activity_web_activiton.*

class WebActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val decorView = window.decorView
        decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        setContentView(R.layout.activity_web_activiton)
        val linkUrl = intent.getStringExtra("link")
        webView.apply {
            @SuppressWarnings
            settings.javaScriptEnabled = true
            webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(
                    view: WebView?,
                    request: WebResourceRequest?
                ): Boolean {

                    try {
                        if (!url.startsWith("http:") || !url.startsWith("https:")) {
//                            val intent = Intent(
//                                Intent.ACTION_VIEW,
//                                Uri.parse(url)
//                            )
//                            startActivity(intent)
                            return true
                        }
                    } catch (e: Exception) {
                        return false
                    }

                    return true
                }
            }
            loadUrl(linkUrl)
        }
    }

    companion object {
        fun activityStart(context: Context, param: String) {
            val intent = Intent(context, WebActivity::class.java)
            intent.putExtra("link", param)
            context.startActivity(intent)
        }
    }
}